package dreadnought;
import javax.swing.*;
import java.awt.*;

/*
* This class represents a players grid
* The grid is 10x10
* Columns are identified by the letters A-L
* Rows are identified by the numbers 1-10
* The grid contains Ship objects
* Initially the grid will contain no Ship objects
* The user will then place 1 of each of the 5 types of Ships
*/
import java.util.Scanner;

public class PlayerGrid extends Grid
{
    public PlayerGrid(Game game)
    {
        //pass game reference to parent construtor
        super(game);
        
        //cells of a the player's grid cannot be clicked
        this.cellsClickable = false;
        
        //add ships to panel
        //add to layer 1 wich is above layer 0
        //GridCells are in layer 0 by default
        for (Ship s : this.ships)
        {
            this.add(s, new Integer(1));
        }

        //probably should remove this later, but i got tired of manually
        //positioning the player's ships during testing
        this.deployShips();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    }

    //this method is called when a remote player or the AI player
    //fires at the players grid. it will determine if the shot hit
    //any ships and update the Ship and GridCell accordingly
    @Override
    public void fire(GridCoordinate coord)
    {
        super.fire(coord);

        //determine the GridCell from the coordinate
        GridCell cell = this.getCellFromCoordinate(coord);

        //try to get the ship at the coordinate of the cell
        //null is returned if no ship is at coordinate
        Ship ship = this.getShipAtCoordinate(coord);

        if (ship != null)
        {
            //handle a hit
            //determine the block id for the block of the ship which was hit
            int blockId = ship.getBlockId(coord);
            //inflict damage to the block which was hit
            ship.inflictDamage(blockId);

            //make the cell display itself as being a hit
            cell.markAsHit();

            //update the game status message
            game.writeToLog("enemy fire " + coord + " (hit)\n");
            

            //check to see if ship has been sunk
            //when a ship is sunk, a message box should be shown
            if (ship.isSunk())
            {
                String msg = "player's "
                        + ship.getType() + " has been sunk!\n";
                game.alert(msg);
                game.writeToLog(msg);
            }
            //check to see if all ships have been sunk
            if (allShipsSunk())
            {
                
                String msg = "All enemy ships have been sunk. player2 wins.";
                game.alert(msg);
                
                game.writeToLog("Gameover. Player2 is Victorious.\n");
                game.setState(GameState.GAMEOVER);
            }
            else
            {
                //the enemy hit a ship so he/she gets to keep playing
                game.setState(GameState.ENEMY_TURN);
            }
        }
        else
        {
            //handle a miss

            //the enemy missed, so it is now the player's turn
            game.setState(GameState.PLAYER_TURN);

            //update the game status message
            game.writeToLog("enemy fire " + coord + " (miss)\n");

            //make the cell display itself as being a miss
            cell.markAsMiss();
        }

        game.turnEvent();
    }
}